package com.app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.app.util.DBHelper;
import com.google.android.material.textfield.TextInputEditText;

public class Register extends AppCompatActivity {

    private TextInputEditText username;
    private TextInputEditText id;
    private TextInputEditText password;
    private TextInputEditText passwordd;
    private AppCompatButton register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

        register.setOnClickListener(v -> {


            String usernameText = username.getText().toString();
            String idText = id.getText().toString();
            String passwordText = password.getText().toString();
            String passworddText = passwordd.getText().toString();

            // 检查输入是否为空
            if (usernameText.isEmpty() || idText.isEmpty() || passwordText.isEmpty() || passworddText.isEmpty()) {
                Toast.makeText(this, "输入不能为空", Toast.LENGTH_SHORT).show();
                return; // 如果输入为空，直接返回，不执行后续的注册操作
            }

            if (username.getText().toString().equals("admin")) {
                Toast.makeText(this, "不能注册管理员账号", Toast.LENGTH_SHORT).show();
            } else if (password.getText().toString().equals(passwordd.getText().toString())) {
                DBHelper dbHelper = new DBHelper(getApplicationContext());
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username", username.getText().toString());
                values.put("user_id", id.getText().toString());
                values.put("password", password.getText().toString());
                long insertResult = db.insert("user", null, values);
                if (insertResult != -1) {
                    Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(this, "注册失败，已有账号", Toast.LENGTH_SHORT).show();
                }
                dbHelper.close();
            } else {
                Toast.makeText(this, "两次密码输入不相同", Toast.LENGTH_SHORT).show();
            }

        });
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setOnClickListener(v -> finish());
        ((TextView) toolbar.getChildAt(1)).setText("注册");

        username = (TextInputEditText) findViewById(R.id.username);
        id = (TextInputEditText) findViewById(R.id.id);
        password = (TextInputEditText) findViewById(R.id.password);
        passwordd = (TextInputEditText) findViewById(R.id.passwordd);
        register = (AppCompatButton) findViewById(R.id.register);
    }
}